package com.example.yl.myapplication22222222222;

import android.content.Context;
import android.support.test.InstrumentationRegistry;
import android.support.test.filters.SdkSuppress;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;
import android.support.test.uiautomator.By;
import android.support.test.uiautomator.Direction;
import android.support.test.uiautomator.UiDevice;
import android.support.test.uiautomator.UiObject2;
import android.support.test.uiautomator.UiObjectNotFoundException;

import junit.framework.Assert;

import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

/**
 * Created by yl on 2018/4/29.
 */
@RunWith(AndroidJUnit4.class)
@SdkSuppress(minSdkVersion = 18)
public class UIAutomator {

    private UiDevice uiDevice;
    @Rule
    public ActivityTestRule<MainActivity_2> mActivityRule = new ActivityTestRule<>(
            MainActivity_2.class);
    private Context context;

    @Before
    public void beforemetoh() {
        context = InstrumentationRegistry.getTargetContext();
        uiDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
//        uiDevice.pressBack();
//        uiDevice.pressHome();
    }

    @Test
    public void dian() throws UiObjectNotFoundException {
//        UiCollection collection = new UiCollection(new UiSelector().resourceId(""));
//        new UiObject(new UiSelector().resourceId(""));//通过ID
//        new UiObject(new UiSelector().textContains(""));//通过包含文本内容
//        new UiObject(new UiSelector().text(""));//精确匹配文本内容
//        new UiObject(new UiSelector().textStartsWith(""));开头文字匹配
//        new UiObject(new UiSelector().textMatches("")); 通过正则表达
//        new UiObject(new UiSelector().resourceIdMatches(".+id/title"));通过ID正则表达
//        new UiObject(new UiSelector().className(""));通过类
//        new UiObject(new UiSelector().classNameMatches(""));通过正则匹配类
//        uiDevice.wait(Until.hasObject(By.text("跳转fffff")), 3000);
//        UiObject2 bt = uiDevice.findObject(By.text("跳转fffff"));
//        bt.click();
//        UiObject bt = new UiObject(new UiSelector().text("跳转fffff"));
        UiObject2 bt2 = uiDevice.findObject(By.text("aaa"));
        String str = bt2.getText();
        bt2.setText("heihei");
        Assert.assertEquals("heihei" + str,  bt2.getText());
    }

    public void findObjects() throws InterruptedException {
//        List<UiObject2> bt = uiDevice.findObjects(By.clazz(Button.class));
//        for (UiObject2 ui : bt) {
//            String str = ui.getText() == null ? "null" : ui.getText();
//            Log.e("btClass", str + ">>>>");
//        }
//        Log.e("btClass", "--------------------------------");
        UiObject2 list = uiDevice.findObject(By.res("com.example.yl.myapplicationaaaa:id/linear"));

//        //获取这个控件所有的娃儿
//        List<UiObject2> listChildren = list.getChildren();
//        for (UiObject2 ui : listChildren) {
//            String str = ui.getText() == null ? "null" : ui.getText();
//            Log.e("btClass", str + ">>>>");
//        }
//        uiDevice.wait(2000);
        //自定义速度，执行一个扫动手势
//        list.fling(Direction.DOWN, 2000);
//        uiDevice.wait(2000);
//        //执行一个滑动操作，可自定义滑动距离和速度
        list.swipe(Direction.UP, 0.5f, 3000);
//        list.setGestureMargin(100);
    }

    @After
    public void after() {

    }
}
